﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface; 

namespace MaxStudio.Concrete.Server.Service
{
    public class ProductService : ServiceHandle, IProduct
    {
        #region IProduct Members

        public OperationResult<List<tb_Product>> GetAllProduct()
        {
            ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

            OperationResult<List<tb_Product>> result = new OperationResult<List<tb_Product>>();
            List<tb_Product> list = new List<tb_Product>();
            try
            {
                list = dataContext.GetAllProduct();
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Product> GetProductById(int id)
        {
            ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

            OperationResult<tb_Product> result = new OperationResult<tb_Product>();
            tb_Product product;
            try
            {
                product = dataContext.GetProductById(id);
                if (product == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = product;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<List<tb_Product>> GetProductByCategoryId(int categoryId)
        {
            ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

            OperationResult<List<tb_Product>> result = new OperationResult<List<tb_Product>>();
            List<tb_Product> list = new List<tb_Product>();
            try
            {
                list = dataContext.GetProductByCategoryId(categoryId);
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Product> CreateProduct(tb_Product entity)
        {
            OperationResult<tb_Product> result = new OperationResult<tb_Product>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateProduct(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Product> ModifyProduct(tb_Product entity)
        {
            OperationResult<tb_Product> result = new OperationResult<tb_Product>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.ModifyProduct(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Product> DeleteProduct(tb_Product entity)
        {
            OperationResult<tb_Product> result = new OperationResult<tb_Product>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteProduct(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        #endregion

        #region IProductMappingWithRM Members


        public OperationResult<tb_Mapping_RMProduct> CreateProductMappingWithRM(tb_Mapping_RMProduct entity)
        {
            OperationResult<tb_Mapping_RMProduct> result = new OperationResult<tb_Mapping_RMProduct>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateMappingWithRM(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }
 
        public OperationResult<tb_Mapping_RMProduct> DeleteProductMappingWithRM(int productId)
        {
            OperationResult<tb_Mapping_RMProduct> result = new OperationResult<tb_Mapping_RMProduct>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteMappingWithRM(productId);
                dataContext.SubmitChanges();
                result.obj = null;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }
        public OperationResult<List<tb_Mapping_RMProduct>> GetProductRMMappingByProductId(int productId)
        {
            ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

            OperationResult<List<tb_Mapping_RMProduct>> result = new OperationResult<List<tb_Mapping_RMProduct>>();
            List<tb_Mapping_RMProduct> list = new List<tb_Mapping_RMProduct>();
            try
            {
                list = dataContext.GetProductRMMappingByProductId(productId);
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        #endregion
    }
}
